import 'dart:async';

import 'package:base_lib/common/bloc/base_bloc.dart';
import 'package:base_lib/common/network/result_body.dart';
import 'package:base_lib/index.dart';
import 'package:base_lib/utils/toast.dart';
import 'package:flutter/cupertino.dart';
import 'package:doctor_login_lib/cache/userinfo_cache.dart';
import 'package:doctor_login_lib/login/entity/doctor_login_entity.dart'
    as DoctorLoginBean;
import 'package:doctor_login_lib/component.dart';
import 'package:doctor_login_lib/repository/http.dart';
import 'package:rxdart/rxdart.dart';

class LoginBloc extends BaseBloc {
  PublishSubject<int> sentSmsCount = PublishSubject();
  int _sentSmsCount = 0;
  String _smsCode = '';
  StreamSubscription _sentSmsCountSubscription;

  String _phone = '', _pwd = '';

  BehaviorSubject<String> historyPhone = BehaviorSubject();

  BehaviorSubject<String> historyPwd = BehaviorSubject();

  ///登录结果流
  BehaviorSubject<DoctorLoginBean.Data> personLoginResult = BehaviorSubject();

  PublishSubject<bool> loadingStatus = PublishSubject();

  PublishSubject<bool> useCodeLogin = PublishSubject();
  PublishSubject<bool> passObscureSubject = PublishSubject();

  bool _defaultLoginByCode = false;
  bool _isPassObscureText = true;

  @override
  void initState() {
    super.initState();
    useCodeLogin.add(_defaultLoginByCode);
    passObscureSubject.add(_isPassObscureText);
  }

  @override
  void dispose() {
    sentSmsCount?.close();
    _sentSmsCountSubscription?.cancel();
    historyPhone?.close();
    historyPwd?.close();
    personLoginResult?.close();
    loadingStatus?.close();
    useCodeLogin?.close();
    passObscureSubject?.close();
  }

  void passwordObscureSubType() {
    _isPassObscureText = !_isPassObscureText;
    passObscureSubject.add(_isPassObscureText);
  }

  void changeLoginType() {
    _defaultLoginByCode = !_defaultLoginByCode;
    useCodeLogin.add(_defaultLoginByCode);
  }

  void editPhoneChanged(String value) {
    _phone = value;
    historyPhone.add(_phone);
  }

  void editPwdChanged(String value) {
    _pwd = value;
    historyPwd.add(_pwd);
  }

  void login(BuildContext context) async {
    historyPhone.add(_phone);
    historyPwd.add(_pwd);
    if (_phone.isEmpty) {
      ToastUtil.show("请输入手机号");
      return;
    }

    if (_pwd.isEmpty) {
      ToastUtil.show("请输入密码");
      return;
    }
    loadingStatus.add(true);
    var result = await LoginHttp.doctorLogin(_phone, _pwd);
    personLoginResult.add(result?.isSuccess == true ? result.data : null);
    loadingStatus.add(false);
    if (result.isSuccess) {
      SPUtil.putString('phone', _phone);
      SPUtil.putString('pwd', _pwd);
      DoctorLoginComponent.dealLoginLogic(context, result.data);
    } else {
      ToastUtil.show(result.error.msg);
    }
  }

  void smsCodeChanged(String value) {
    _smsCode = value;
  }

  ///发送验证码
  void sentSms() async {
    if (_phone.isEmpty) {
      ToastUtil.show("请输入手机号码!");
      return;
    }
    if (_sentSmsCount != 0) return;
    loadingStatus.add(true);
    var body = await LoginHttp.getCode(_phone);
    loadingStatus.add(false);
    if (!body.isSuccess) {
      ToastUtil.show(body.error.msg);
      return;
    } else {
      ToastUtil.show(body.error.msg);
    }
    _sentSmsCount = 60;
    _sentSmsCountSubscription = Stream.periodic(Duration(seconds: 1))
        .map((value) => _sentSmsCount - 1)
        .listen((value) {
      _sentSmsCount = value;
      sentSmsCount.add(_sentSmsCount);
      if (_sentSmsCount == 0) _sentSmsCountSubscription.cancel();
    });
  }

  ///通过验证码登录
  void loginByCode(BuildContext context) async {
    if (_phone.isEmpty) {
      ToastUtil.show("请输入手机号码!");
      return;
    }
    if (_smsCode.isEmpty) {
      ToastUtil.show("请输入手机号码!");
      return;
    }
    loadingStatus.add(true);
    var result = await LoginHttp.doctorLoginByCode(_phone, _smsCode);
    if (result.isSuccess) {
      DoctorLoginComponent.dealLoginLogic(context, result.data,
          isLoginByCode: true);
    } else {
      ToastUtil.show(result?.error?.msg ?? "");
    }
    loadingStatus.add(false);
  }

  void jump2ForgotPassword(BuildContext context) {
    Routers.navigateTo(context, DoctorLoginComponent.forgotPwdPage);
  }

  void jump2Register(BuildContext context) {
    Routers.navigateTo(context, DoctorLoginComponent.registerPhonePage);
  }
}
